package mapper

import (
	"com.yy.api/server/models"
	"strings"
)

type ContainerMapper interface {
	GenericMapper[models.ContainerBasic]
	ListHostPortMappings() (string, error)
}

type ContainerMapperImpl struct {
	*GenericMapperImpl[models.ContainerBasic]
}

func (m *ContainerMapperImpl) ListHostPortMappings() (string, error) {
	var containers []models.ContainerBasic //[["4000:80","4001:443"],["4002:80","4003:443"]] --> ["4000","4001","4002","4003"]
	err := m.DB.Find(&containers).Error
	if err != nil {
		return "", err
	}
	var result []string
	for _, container := range containers {
		if container.PortMappings != nil && len(container.PortMappings) > 0 {
			for _, portMapping := range container.PortMappings {
				result = append(result, portMapping.HostPort)
			}
		}
	}
	return strings.Join(result, ","), err
}
